package com.lc.luzhengtang.adapter;

import android.content.Intent;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.lc.luzhengtang.R;
import com.lc.luzhengtang.activity.ShopDetailActivity;
import com.lc.luzhengtang.bean.CommenBean;
import com.lc.luzhengtang.bean.ShopCarListBean;
import com.lc.luzhengtang.conn.Conn;
import com.lc.luzhengtang.conn.GetUpdateCartGoodsNum;
import com.lc.luzhengtang.util.LovelyToast;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.scale.ScaleScreenHelperFactory;

import java.util.List;


/**
 * Created by Administrator on 2017/1/22.
 */

public class ShopCarAdapter extends BaseQuickAdapter<ShopCarListBean.DataBeanX.DataBean, BaseViewHolder> {

    //    SmoothCheckBox mScb;
    private TextView tvGoodsNum;

    public Listener listener;
    private String flag = null;

    CheckBox img_address_add;


    LinearLayout item_shopping_car_good_select;


    CheckBox checkBox;

    public ShopCarAdapter(List<ShopCarListBean.DataBeanX.DataBean> data) {
        super(R.layout.item_shoppingcar, data);
    }

    public void setListener(Listener listener) {
        this.listener = listener;
    }


    @Override
    protected void convert(final BaseViewHolder helper, final ShopCarListBean.DataBeanX.DataBean item) {
        ScaleScreenHelperFactory.getInstance().loadView((ViewGroup) helper.convertView);
        helper.setText(R.id.text_nicheng, item.getTitle());
        helper.setText(R.id.text_money, item.getMarket_price());
        Glide.with(mContext).load(Conn.HOST2 + item.getPicurl()).crossFade().into((ImageView) helper.getView(R.id.img_index));
        helper.setText(R.id.tv_goods_num, item.getNumber() + "");
        helper.setText(R.id.tv_goods_money, item.getMarket_price());

//        mScb = helper.getView(R.id.checkbox_item_selected);
        ImageButton ibUp = helper.getView(R.id.ib_up);
        ImageButton ibDown = helper.getView(R.id.ib_down);
        helper.addOnClickListener(R.id.img_delete);
        View view = helper.getConvertView();
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.putExtra("goods_id", item.getId() + "");
                intent.setClass(mContext, ShopDetailActivity.class);
                mContext.startActivity(intent);
            }
        });

        checkBox = helper.getView(R.id.img_address_add);
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
              /*  if (isChecked) {
//                    viewHolder.iv_edit_circle.setSelected(list.get(position).isChoose());
                    item.setChoose(true);
                    listener.startCalculate();
                    item.setSelected(isChecked);
//                    checkBox.setBackground(mContext.getResources().getDrawable(R.mipmap.checkboxselected));

                    listener.checkAll(helper.getAdapterPosition(), isChecked);
                } else {
                    item.setChoose(false);
//                    checkBox.setBackground(mContext.getResources().getDrawable(R.mipmap.checkboxunselected));
                }*/
            }
        });
//        checkBox.setChecked(item.getSelected());


       /* img_address_add=helper.getView(R.id.img_address_add);
        img_address_add.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    img_address_add.setSelected(true);
                }
            }
        });*/


        //设置tag防止数据混乱
       /* helper.setTag(R.id.text_nicheng, helper.getAdapterPosition());

        helper.setTag(R.id.text_money, helper.getAdapterPosition());
        helper.setTag(R.id.img_index, helper.getAdapterPosition());
        helper.setTag(R.id.tv_goods_num, helper.getAdapterPosition());
        helper.setTag(R.id.tv_goods_money, helper.getAdapterPosition());*/


        //数量加减
        ibUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int i = item.getNumber() + 1;
                item.setNumber(i);
                helper.setText(R.id.tv_goods_num, String.valueOf(i));
                requestChangeNum(helper, item);
                flag = "0";


            }
        });
        ibDown.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (item.getNumber() > 1) {
                    int i = item.getNumber() - 1;
                    item.setNumber(i);
                    helper.setText(R.id.tv_goods_num, String.valueOf(i));
                    requestChangeNum(helper, item);
                    //    flag = "1";
                }
            }
        });
/*
//        mScb.setChecked(item.getSelected(), false);
        mScb.setOnCheckedChangeListener(new SmoothCheckBox.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(SmoothCheckBox checkBox, boolean isChecked) {

                item.setSelected(isChecked);//如果选中则设置bean中字段为ture
//                listener.startCalculate();
                item.setSelected(isChecked);
//                listener.startCalculate();
//                listener.checkAll(helper.getAdapterPosition(), isChecked);
            }
        });
//        mScb.setChecked(item.isSelected(), false);
        mScb.setChecked(item.getSelected(), false);*/


    }


    private void requestChangeNum(final BaseViewHolder helper, final ShopCarListBean.DataBeanX.DataBean item) {

        new GetUpdateCartGoodsNum(String.valueOf(item.getId()), String.valueOf(item.getId()), String.valueOf(item.getNumber()), new AsyCallBack<CommenBean>() {
            @Override
            public void onSuccess(String toast, int type, Object o, CommenBean commenBean) throws Exception {
                super.onSuccess(toast, type, o, commenBean);
                listener.startCalculate();
                Toast.makeText(mContext, "成功", Toast.LENGTH_SHORT).show();

            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);

                LovelyToast.makeText(mContext, "更改失败", 0, LovelyToast
                        .SUCCESS, LovelyToast.SCALE);
                switch (flag) {
                    case "0":
                        item.setNumber(item.getNumber() - 1);
                        break;
                    case "1":
                        item.setNumber(item.getNumber() + 1);
                        break;
                }
            }


        }).execute(mContext, false);
    }

    public interface Listener {
        void startCalculate();

        void checkAll(int position, boolean isCChecked);
    }
}
